﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace RevolioReader
{
    public class ScanLog
    {
        private string logFileName;
        private Dictionary<int, ScanLogEntry> entries;

        public ScanLog(string filename)
        {
            this.logFileName = filename;
            this.entries = new Dictionary<int, ScanLogEntry>();

            // Open and read the log file.
            using (FileStream stream = new FileStream(
                filename, FileMode.OpenOrCreate, FileAccess.Read))
            using (StreamReader reader = new StreamReader(stream))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    string[] elements = line.Split(new char[] { '\t' });
                    if (elements.Length < 2)
                        continue;

                    int id;
                    if (!Int32.TryParse(elements[0], out id) || id <= 0)
                        continue;

                    BookAvailability avail;
                    if (!Enum.TryParse<BookAvailability>(elements[1], true, out avail))
                        continue;

                    ScanLogEntry ent = new ScanLogEntry();
                    ent.Id = id;
                    ent.Availability = avail;

                    if (avail == BookAvailability.Available && elements.Length == 5)
                    {
                        ent.Title = elements[2];
                        ent.SubTitle = elements[3];
                        Int32.TryParse(elements[4], out ent.PageCount);
                    }
                    else if (elements.Length > 2)
                    {
                        ent.Message = elements[2];
                    }
                    entries[id] = ent;
                }
            }
        }

        public void AddEntry(ScanLogEntry ent)
        {
            using (Stream stream = new FileStream(logFileName, FileMode.Append, FileAccess.Write))
            using (StreamWriter writer = new StreamWriter(stream))
            {
                string line = ent.Id + "\t" + ent.Availability;
                if (ent.Availability == BookAvailability.Available)
                {
                    line += string.Format("\t{0}\t{1}\t{2}",
                         ent.Title, ent.SubTitle, ent.PageCount);
                }
                else if (ent.Message != null)
                {
                    line += "\t" + ent.Message;
                }
                writer.WriteLine(line);
            }
            entries[ent.Id] = ent;
        }

        public IEnumerable<ScanLogEntry> Entries
        {
            get
            {
                return from id in entries.Keys orderby id select entries[id];
            }
        }

        public ScanLogEntry GetEntry(int id)
        {
            ScanLogEntry ent;
            return entries.TryGetValue(id, out ent) ? ent : null;
        }
    }

    public class ScanLogEntry
    {
        public int Id;
        public BookAvailability Availability;
        public string Message;
        public string Title;
        public string SubTitle;
        public int PageCount;
    }
}
